$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        type: null,
        init:function () {
            this.dataGrid = new xhDataGrid($('#list') , 'Order/getOrderList/tablet');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_deliver','bt_list_check','bt_list_cancel','bt_list_reject']);
            this.funObj().bindDataGridEvent();
            if($('#bt_list_checkMenu').length >= 1){
                this.funObj().initMenu();
            }
            this.listener();
        },
        listener:function () {
            var $this = this;

            //确认发货
            $('#bt_list_deliver').on('click',function () {
                if (xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().checkAsk();
            });

            //搜索
            $('#bt_list_search').on('click',function () {
                $this.funObj().search();
            });

            //平板订单审核
            // $('#bt_list_check').on('click',function () {
            //     if (xhExtra.buttonDisabled($(this))){
            //         return false;
            //     }
            //     $this.funObj().checkOrder();
            // });

            //拒绝申请
            // $('#bt_list_reject').on('click',function () {
            //     if (xhExtra.buttonDisabled($(this))){
            //         return false;
            //     }
            //     $this.funObj().rejectCheck();
            // });

            //取消订单
            $('#bt_list_cancel').on('click',function () {
                if (xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().cancelOrder();
            });
        },
        funObj:function () {
            var $this = this;
            return {
                bindDataGridEvent:function () {
                    $this.dataGrid.command({
                        view:detailview,
                        detailFormatter:function(index,row){
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;"  ><table style="width: 100%;height: 100%"  >';
                            if(row['payProof'] != undefined){
                                str+='<tr  class="imgList" nodeId="'+row['id']+'"><td colspan="2" style="border: 0px;">'+timeLine.showImage(row['payProof']+',',row['id'],'',false)+'</td></tr>';
                            }
                            str+='</table></div>';
                            return str;
                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            ddv.panel({
                                // height:200,
                                width:780,
                                border:false,
                                cache:false,
                                onLoad:function(){
                                    $this.dataGrid.command('fixDetailRowHeight',index);
                                    $this.dataGrid.command('fixDetailRowWidth',index);
                                }
                            });
                            $this.dataGrid.command('fixDetailRowHeight',index);
                        }
                    });
                    $this.dataGrid.onLoadSuccess = function () {//设置imgView的点击事件
                        $('.images').viewer();//设置图片点击放大
                        var row = $this.dataGrid.command("getRows");
                        for (var r = 0; r < row.length; r++)
                        {
                            if(row[r]['payProofStatus'] == '等待审核'){//展开等待审核
                                $this.dataGrid.command("expandRow",r);
                            }
                        }
                    }
                },
                checkAsk:function () {
                    var no = $this.dataGrid.getSelectValue('no');
                    var state = $this.dataGrid.getSelectValue('status');
                    if(state == '待发货' ) {
                        window.top.xhTop.showModal({
                            title: '发货确认',
                            content: '<iframe  src="'+baseUrl+'order/deliverForm/'+no+'"  style="width: 100%;;border-style: none;padding: 0px;height:200px"  scrolling="yes"   ></iframe>',
                            button:'提交'
                        });
                    }else {
                        window.top.xhTop.showError('错误状态');
                    }
                },
                checkOrder:function () {
                    var no = $this.dataGrid.getSelectValue('no');
                    var payProofTradeNo = $this.dataGrid.getSelectValue('payProofTradeNo');
                    var state = $this.dataGrid.getSelectValue('payProofStatus');
                    if( state == '等待审核' ){
                        xhExtra.askChangeStatus('order/passOrder',{no:no,payProofTradeNo:payProofTradeNo},null,'确定要审核通过？',function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.dataGrid.reload();
                        });
                    }else  {
                        window.top.xhTop.showError('错误状态');
                    }
                },
                rejectCheck:function () {
                    var no = $this.dataGrid.getSelectValue('no');
                    var state = $this.dataGrid.getSelectValue('payProofStatus');
                    if( state == '等待审核' ){
                        window.top.xhTop.showModal({
                            title: '拒绝申请',
                            content: '<iframe  src="'+baseUrl+'order/rejectForm/'+no+'"  style="width: 100%;;border-style: none;padding: 0px;height:200px"  scrolling="yes"   ></iframe>',
                            button:'提交'
                        });
                    }else  {
                        window.top.xhTop.showError('错误状态');
                    }

                },
                cancelOrder:function () {
                    var no = $this.dataGrid.getSelectValue('no');
                    var proofStatus = $this.dataGrid.getSelectValue('payProofStatus');
                    var status      = $this.dataGrid.getSelectValue('status');
                    if( status == '未支付'  && proofStatus !='等待审核'){
                        window.top.xhTop.showModal({
                            title: '取消订单',
                            content: '<iframe  src="'+baseUrl+'order/cancelForm/'+no+'"  style="width: 100%;;border-style: none;padding: 0px;height:200px"  scrolling="yes"   ></iframe>',
                            button:'提交'
                        });
                    }else  {
                        window.top.xhTop.showError('错误状态');
                    }
                },
                search : function () {
                    var key = xhExtra.getValue($('#list_search'));
                    var orderState = xhExtra.getValue($('#select_orderState'));
                    var proofState = xhExtra.getValue($('#select_proofState'));
                    $this.dataGrid.reload({key:key,orderState:orderState,proofState:proofState});
                },
                initMenu:function () {
                    var menu = $('#bt_list_checkMenu').menubutton({
                        iconCls: 'fa fa-bars fa-lg',
                        menu: '#checkMenu'
                    });
                    $(menu.menubutton('options').menu).menu({
                        onClick: function (item) {
                            //item 的相关属性参见API中的menu、
                            var selections = $this.dataGrid.command('getSelections');
                            if(selections.length > 1 || selections.length <= 0 ){
                                window.top.xhTop.showError('错误','请选择一个订单进行操作');
                                return false;
                            }
                            if(item.text=='接受凭证'){
                                $this.funObj().checkOrder();
                            }else if(item.text=='拒绝凭证'){
                                $this.funObj().rejectCheck();
                            }else if(item.text=='接受退款'){
                                $this.funObj().agreeRefund();
                            }else if(item.text=='拒绝退款'){
                                $this.funObj().refuseRefund();
                            }
                        }
                    })
                },
                agreeRefund:function () {
                    var no = $this.dataGrid.getSelectValue('no');
                    var state = $this.dataGrid.getSelectValue('payProofStatus');
                    if( state == '退款中' ){
                        window.top.xhTop.showModal({
                            title: '同意退款',
                            content: '<iframe  src="'+baseUrl+'order/refundAgree/'+no+'"  style="width: 100%;;border-style: none;padding: 0px;height:200px"  scrolling="yes"   ></iframe>',
                            button:'提交'
                        });
                    }else  {
                        window.top.xhTop.showError('错误状态');
                    }
                },
                refuseRefund:function () {
                    var no = $this.dataGrid.getSelectValue('no');
                    var state = $this.dataGrid.getSelectValue('payProofStatus');
                    if( state == '退款中' ){
                        window.top.xhTop.showModal({
                            title: '拒绝退款',
                            content: '<iframe  src="'+baseUrl+'order/refundRefuse/'+no+'"  style="width: 100%;;border-style: none;padding: 0px;height:200px"  scrolling="yes"   ></iframe>',
                            button:'提交'
                        });
                    }else  {
                        window.top.xhTop.showError('错误状态');
                    }
                }
            }
        }
    }
};